﻿/*選擇上级机构*/


/********************************************************************/
/*未選*/
var fieldParentOrg = [{
       name: "ID", type: "string"
   }, {
       name: "OrgName", type: "string"
   }, {
       name: "RoleType", type: "string"
   }];
var pageSize =16;
var StoreParentOrg = Ext.create('Ext.data.Store', {
    remoteSort: false,
    fields: fieldParentOrg,
    pageSize: pageSize,
    model: 'ForumThread',
    proxy: {
        type: 'ajax',
        url: "OrgnizationMag.aspx?action=GetParentOrg",
        actionMethods: 'post',
        reader: {
            type: 'json',
            root: 'data',
            totalProperty: "totalCount"
        }
    }
});

var btn_Server = Ext.create('Ext.button.Button', {
    text: '查詢',
    handler: function () { StoreParentOrg.load(); }
});


var smParentOrg = Ext.create('Ext.selection.CheckboxModel', {
    dataIndex: "ID"
});
var gridParentOrg = Ext.create('Ext.grid.Panel', {
    selModel: smParentOrg,
    id: "gridParentOrg",
    baseCls: "x-panel",
    border: 0,
    autoHeight: true,
    store: StoreParentOrg,
    columns: [{ header: 'ID', dataIndex: 'ID', hidden: true },
                { header: '机构名称', dataIndex: 'OrgName' },
                { header: '机构类型', dataIndex: 'RoleType' }],
    dockedItems: [{
        xtype: 'toolbar',
        items: ['名称',
                {
                    xtype: 'textfield',
                    name: 'searchOrgName',
                    id: 'searchOrgName',
                    hideLabel: true,
                    width: 150
                }, {
                    xtype: 'combobox',
                    fieldLabel: '级别类型',
                    border: 0,
                    labelWidth: 60,
                    name: 'searchRoleType',
                    id: 'searchRoleType',
                    editable: false,
                    store: RoleTypeStore,
                    valueField: 'value',
                    displayField: 'name'
                }, btn_Server
               ]

    }],
    bbar: Ext.create('Ext.PagingToolbar', {
        store: StoreParentOrg,
        pageSize: 16,
        displayInfo: true,
        displayMsg: '分頁 {0} - {1} of {2}',
        emptyMsg: "No topics to display",
        prevText: "上一頁",
        nextText: "下一頁",
        refreshText: "刷新",
        lastText: "最後一頁",
        firstText: "第一頁",
        beforePageText: "當前頁",
        afterPageText: "共{0}頁"
    })
});
StoreParentOrg.on("beforeload", function () {
    Ext.apply(StoreParentOrg.proxy.extraParams,
         {
             OrgName: Ext.getCmp('searchOrgName').getValue(),
             RoleType: Ext.getCmp('searchRoleType').getValue()
         }
         );
});
     StoreParentOrg.load({ params: { start: 0, limit: pageSize} });

     var ParentOrg = function () {
         /********************************************************************/
         /*佈局*/
         var ParentOrgWin = Ext.create('Ext.window.Window', {
             title: '選擇上级机构',
             width: 480,
             height: 365,
             layout: 'fit',
             labelWidth: 100,
             closeAction: 'hide',
             resizable: false,
             modal: 'true',
             iconCls: "addicon",
             items: gridParentOrg,
             listeners: {
                 "show": function () {

                 }
             },
             buttonAlign: "center",
             buttons: [{
                 text: '確定',

                 handler: function () {
                     /*保存選項,產生json*/
                     var row = Ext.getCmp("gridParentOrg").getSelectionModel().getSelection();
                     if (row.length == 0) {
                         ParentID = "";
                         ParentName = "";
                         Ext.getCmp('frmAdd').getForm().findField('ParentID').setValue(ParentID);
                         Ext.getCmp('frmAdd').getForm().findField('ParentName').setValue(ParentName);
                         Ext.getCmp('frmEdit').getForm().findField('ParentID').setValue(ParentID);
                         Ext.getCmp('frmEdit').getForm().findField('ParentName').setValue(ParentName);
                         ParentOrgWin.hide();
                     }
                     else if (row.length > 1) {
                         Ext.Msg.alert("提示信息", "只能選中一個關聯課程!");
                     }
                     else {
                         ParentID = row[0].data.ID;
                         ParentName = row[0].data.OrgName;
                         Ext.getCmp('frmAdd').getForm().findField('ParentID').setValue(ParentID);
                         Ext.getCmp('frmAdd').getForm().findField('ParentName').setValue(ParentName);
                         Ext.getCmp('frmEdit').getForm().findField('ParentID').setValue(ParentID);
                         Ext.getCmp('frmEdit').getForm().findField('ParentName').setValue(ParentName);
                         ParentOrgWin.hide();
                     }
                 }
             }]
         });
         ParentOrgWin.show();
     }